import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import Cookies from 'js-cookie'
import store from '@/store'
import iView from 'iview'
import { setToken, getToken, getId,canTurnTo, setTitle } from '@/libs/util'
import config from '@/config'
const { homeName } = config

Vue.use(Router)
const router = new Router({
  routes,
  hash: 'history'
})
const LOGIN_PAGE_NAME = 'login'

const turnTo = (to, access, next) => {
  if (canTurnTo(to.name, access, routes)){
    store.state.routerNew = to.name
    Cookies.set('routerNew', to.name, { expires: 1 })
    next()// 有权限，可访问
  } else{
    next({ replace: true, name: 'error_401' }) // 无权限，重定向到401页面
  }
}

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
  const id = getId()
  var token = getToken()
  if(id&&!token){
    if(id=="fd246479739a4b09a19d136dfb382995") {
      token = 'super_admin'
    } else {
      token = 'admin'
    }
  }
  if (!token && to.name !== LOGIN_PAGE_NAME) {
    // 未登录且要跳转的页面不是登录页
    next({
      name: LOGIN_PAGE_NAME // 跳转到登录页
    })
  } else if (!token && to.name === LOGIN_PAGE_NAME) {
    // 未登陆且要跳转的页面是登录页
    next() // 跳转
  // } else if (!id&&token && to.name === LOGIN_PAGE_NAME) {
  } else if (token && to.name === LOGIN_PAGE_NAME) {
    if (Cookies.get('routerNew')) {
      next({
        name: Cookies.get('routerNew') // 跳转到homeName页
      })
    } else {
      // 已登录且要跳转的页面是登录页
      next({
        name: homeName // 跳转到homeName页
      })
    }


  // } else if(id) {
  //   next()
  } else {
    if (store.state.user.hasGetInfo) {
      turnTo(to, store.state.user.access, next)
    } else {
      store.dispatch('getUserInfo').then(user => {
        if(user.rid=="b20e23a58fee405da36647f981152373") {
          user.access = 'super_admin'
        } else {
          user.access = 'admin'
        }
        // 拉取用户信息，通过用户权限和跳转的页面的name来判断是否有权限访问;access必须是一个数组，如：['super_admin'] ['super_admin', 'admin']
        turnTo(to, user.access, next)
      }).catch(() => {
        setToken('')
        next({
          name: 'login'
        })
      })
    }
  }
})

router.afterEach(to => {
  setTitle(to, router.app)
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

export default router
